package main

import (
	"fmt"
	"os"
	"bufio"
	"gxgeek.com/gxgeek/learngo/lang/functionalional/fib"
	"errors"
)

func tryDefer()  {
	//defer fmt.Println(1)
	//defer fmt.Println(2)
	//fmt.Println(3)
	//panic("error ")
	//fmt.Println(4)
	for i:=0;i<100 ;i++  {
		defer fmt.Println(i)
		if i==30 {
			panic("print to many")
		}
	}
}

func writeFile(filename string)  {
	file, error := os.OpenFile(filename, os.O_EXCL|os.O_CREATE, 0666)
	//file, error := os.Create(filename)
	error = errors.New("this is a custom error")
	if error !=nil {
		//fmt.Println("file already exists")
		if pathError ,ok := error.(* os.PathError); !ok{
			panic(error)
		}else {
			fmt.Printf("%s ,%s ,%s\n",pathError.Op,pathError.Path,pathError.Err)
		}
		return
		//panic(error)
	}
	defer file.Close()

	writer := bufio.NewWriter(file)
	defer writer.Flush()
	f := fib.Fibonacci()
	for i:=0;i<20 ;i++  {
		fmt.Fprintln(writer,f())
	}

}
func main() {
	//tryDefer()
	writeFile("fib.txt")
}

